package com.xiu.travel.controller;

import com.xiu.travel.other.SysRoleConvertor;
import com.xiu.travel.other.SysRoleDTO;
import com.xiu.travel.other.SysRoleVO;
import com.xiu.travel.util.ControllerPageParmaAppendUtil;
import com.xiu.travel.util.api.CommonResult;
import com.xiu.travel.util.api.ResultCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiu.travel.entity.SysRole;
import com.xiu.travel.exception.Asserts;
import com.xiu.travel.security.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author YuLing
 * @since 2023-10-13
 */
@RestController
@RequestMapping("/admin/api/sys-role")
@Tag(name = "系统角色表接口")
public class SysRoleController
{

    /**
     * service
     */
    @Autowired
    private SysRoleService service;


    /**
     * 保存
     *
     * @param dto 参数
     * @return 保存结果
     */
    @PostMapping
    @Operation(summary = "添加一个系统角色表")
    public CommonResult<String> saveOne(@Validated @RequestBody SysRoleDTO dto)
    {
        SysRole entity = SysRoleConvertor.INSTANCE.toEntity(dto);
        if ( !this.service.save(entity) )
        {
            Asserts.fail(ResultCode.SQL_INSERT_FAILED);
        }
        return CommonResult.success(entity.getId() + "");
    }

    /**
     * 根据主键查询VO
     *
     * @param id 主键
     * @return VO
     */
    @GetMapping
    @Operation(summary = "通过id查询系统角色表")
    public CommonResult<SysRoleVO> getById(@Validated @NotEmpty Long id)
    {
        return CommonResult.success(this.service.getByPk(id));
    }


    /**
     * 根据主键删除
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据主键删除系统角色表")
    public CommonResult<Object> deleteById(@NotEmpty @PathVariable("id") Long id)
    {
        return CommonResult.success(this.service.deleteByPk(id));
    }


    /**
     * 根据主键修改
     *
     * @param dto 类
     * @return 修改结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "根据主键修改系统角色表")
    public CommonResult<Boolean> updateById(@RequestBody SysRoleDTO dto)
    {
        return CommonResult.success(this.service.updateById(dto));
    }


    /**
     * 分页查询
     *
     * @return CommonResult
     */
    @PostMapping("page")
    @Operation(summary = "分页获取系统角色表")
    public CommonResult<Page<SysRole>> listPage(@Parameter(name = "分页参数") @RequestBody Page<SysRole> page) throws IllegalAccessException
    {
        return CommonResult.success(service.getBaseMapper().selectPage(page, ControllerPageParmaAppendUtil.getQueryWrapper(page)));

    }
}